const initialState = {
    list: [],
    per_page: 15,
    current_page: 0,
    total: '',
    detail: {
            id: '',
            project_id: '',
            title: '',
            number: '', 
            content: '', 
            user: {}, 
            project_user_id: '',
            project_category: {},
            project_progress: [],
            project_milestone: [],
            child: [],
            user_point: []
    },
    user_parent_projects: [],
    children: []
};

export const actionsTypes = {
    GET_PRO_LIST: "GET_PRO_LIST",
    RESPONSE_PRO_LIST: "RESPONSE_PRO_LIST",

    GET_DETAIL: "GET_DETAIL",
    RESPONSE_DETAIL: "RESPONSE_DETAIL",

    SAVE_PROJECT: "SAVE_PROJECT",

    GET_USER_PAARENT_PROJECT_LIST: "GET_USER_PAARENT_PROJECT_LIST",
    RESPONSE_USER_PAARENT_PROJECT_LIST: "RESPONSE_USER_PAARENT_PROJECT_LIST",

    ACCEPT_PROJECT: "ACCEPT_PROJECT",

    GET_CHILDREN: "GET_CHILDREN",
    RESPONSE_CHILDREN: "RESPONSE_CHILDREN",

    DELETE_PROJECT: "DELETE_PROJECT",
    CANCEL_PROJECT: "CANCEL_PROJECT",

    PUBLISH_PROJECT: "PUBLISH_PROJECT",

    COMPLETE_PROJECT: "COMPLETE_PROJECT",

    REJECT_PROJECT: "REJECT_PROJECT"
};

export const actions = {
    get_list: function (_type, page=1, search='') {
        return {
            type: actionsTypes.GET_PRO_LIST,
            _type,
            page,
            search

        }
    },
    get_detail: function (id, _type) {
        return {
            type: actionsTypes.GET_DETAIL,
            id,
            _type,
        }
    },
    save_project: function (data, mold, id) {
        return {
            type: actionsTypes.SAVE_PROJECT,
            data,
            mold,
            id
        }
    },
    // 获取上级项目
    get_user_parent_projects: function () {
        return {
            type: actionsTypes.GET_USER_PAARENT_PROJECT_LIST
        }
    },
    accept_project: function (id) {
        return {
            type: actionsTypes.ACCEPT_PROJECT,
            id
        }
    },
    get_children: function(project_id) {
        return {
            type: actionsTypes.GET_CHILDREN,
            project_id
        }
    },
    delete_project: function(project_id) {
        return {
            type: actionsTypes.DELETE_PROJECT,
            project_id
        }
    },
    cancel_project: function(project_id, role='publish') {
        // project_id 可能是project_id 或者 project_user_id
        return {
            type: actionsTypes.CANCEL_PROJECT,
            project_id,
            role
        }
    },
    publish_project: function(project_id) {
        return {
            type: actionsTypes.PUBLISH_PROJECT,
            project_id
        }
    },
    complete_project: function(id, project_id) {
        return {
            type: actionsTypes.COMPLETE_PROJECT,
            id,
            project_id,
        }
    },
    reject_project: function(id) {
        return {
            type: actionsTypes.REJECT_PROJECT,
            id
        }
    },
}

export function reducer(state=initialState, action) {
    switch (action.type) {
        case actionsTypes.RESPONSE_PRO_LIST:
            let list = []
            console.log(action)
            if (action._type=='publish') {
                list = action.data.data.map(x=>{
                    x.project_id = x.id
                    return x
                })
            } else {
                list = action.data.data.map(x=>{
                    x.project.status=x.type   
                    x.project.project_id=x.project.id                
                    x.project.id = x.id
                    x.project.income = x.income
                    return x.project
                })
            }
            return {
                ...state,
                list: list,
                per_page: action.data.per_page,
                current_page: action.data.current_page,
                total: action.data.total

            }
        case actionsTypes.RESPONSE_DETAIL:
            console.log(action)
            let detail = {}
            if (action._type=='publish') {
                action.data.project_id = action.data.id
                detail = action.data
            } else {
                action.data.project.project_id=action.data.project.id
                action.data.project.id=action.data.id
                action.data.project.status = action.data.type
                action.data.project.income = action.data.income
                detail = action.data.project
            }
            return {
                ...state, 
                detail: detail
            }   
        case actionsTypes.RESPONSE_USER_PAARENT_PROJECT_LIST:
            return {
                ...state,
                user_parent_projects: action.data
            }
        case actionsTypes.RESPONSE_CHILDREN:
            return {
                ...state,
                children: action.data
            }
        default:
            return state
    }
}